﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Configuration;
using Model;
using Utility.ExtensionFunc;

namespace DAL
{
    public class OrderRepository : RepositoryBase
    {
        private static OrderRepository orderRepo;

        private OrderRepository()
        {
            xmlDoc = new XmlDocument();
            xmlPath = basicPath + ConfigurationManager.AppSettings["OrderPath"].ToString();
        }

        public static OrderRepository Instrance
        {
            get
            {
                if (null == orderRepo) { orderRepo = new OrderRepository(); }
                return orderRepo;
            }
        }

        public List<ORDER> LoadOrderXML()
        {
            lock (thisLock)
            {
                List<ORDER> orderList = new List<ORDER>();
                base.LoadXML();
                orderList = xmlRoot.ConvertToEntity<ORDER>(ORDER.NODE);
                return orderList;
            }
        }

        public bool AddOrder(ORDER order)
        {
            lock (thisLock)
            {
                List<ORDER> orderList = this.LoadOrderXML();
                if (orderList.Count(c => c.ID == order.ID) > 0)
                {
                    return false;
                }
                XmlElement newOrderElem = order.ConvertToXML<ORDER>(xmlDoc, ORDER.NODE);
                if (null == newOrderElem)
                {
                    return false;
                }
                xmlRoot.AppendChild(newOrderElem);
                this.SaveXML();
                return true;
            }

        }

        public bool OrderItem(string orderID, ITEM item)
        {
            lock (thisLock)
            {
                if (string.IsNullOrEmpty(orderID) || null == item)
                {
                    return false;
                }
                bool existsOrder = false;
                int maxItemId = 0;
                XmlNode orderElem = xmlDoc.CreateElement(ORDER.NODE);
                this.LoadXML();
                foreach (XmlNode node in xmlRoot.ChildNodes)
                {
                    if (node.SelectSingleNode(ORDER.NODE_ID).InnerText != orderID)
                    {
                        continue;
                    }
                    existsOrder = true;
                    orderElem = node;
                }
                if (!existsOrder)
                {
                    ORDER order = new ORDER();
                    order.ID = int.Parse(orderID);
                    orderElem = order.ConvertToXML<ORDER>(xmlDoc, ORDER.NODE);
                    if (null == orderElem)
                    {
                        return false;
                    }
                    xmlRoot.AppendChild(orderElem);
                }
                XmlNode orderItemListElem = orderElem.SelectSingleNode(ORDER.NODE_ITEM_LIST);
                List<ITEM> itemList = orderItemListElem.ConvertToEntity<ITEM>(ITEM.NODE);
                if (null != itemList && itemList.Count > 0)
                {
                    maxItemId = itemList.Max(c => c.ID);
                }

                item.ID = maxItemId + 1;
                XmlElement newItemElem = item.ConvertToXML<ITEM>(xmlDoc, ITEM.NODE);
                if (null == newItemElem)
                {
                    return false;
                }
                orderItemListElem.AppendChild(newItemElem);
                this.SaveXML();
                return true;
            }

        }

        public List<ITEM> LoadOrderItemList(string orderID)
        {
            lock (thisLock)
            {
                List<ITEM> itemList = new List<ITEM>();
                base.LoadXML();
                foreach (XmlNode node in xmlRoot)
                {
                    if (node.SelectSingleNode(ORDER.NODE_ID).InnerText != orderID)
                    {
                        continue;
                    }
                    itemList = node.ConvertToEntity<ITEM>(ITEM.NODE);
                }
                return itemList;
            }
        }

        public List<ITEM> LoadItemList()
        {
            lock (thisLock)
            {
                List<ITEM> itemList = new List<ITEM>();
                base.LoadXML();
                itemList = xmlRoot.ConvertToEntity<ITEM>(ITEM.NODE);
                return itemList;
            }
        }

        public bool DeleteItem(string orderID, string itemID)
        {
            lock (thisLock)
            {
                this.LoadXML();
                XmlNode deleteNode;
                foreach (XmlNode node in xmlRoot.SelectNodes(ORDER.NODE))
                {

                    if (node.SelectSingleNode(ORDER.NODE_ID).InnerText != orderID)
                    {
                        continue;
                    }

                    foreach (XmlNode item in node.SelectSingleNode(ORDER.NODE_ITEM_LIST).SelectNodes(ITEM.NODE))
                    {
                        if (item.SelectSingleNode(ITEM.NODE_ID).InnerText == itemID)
                        {
                            node.SelectSingleNode(ORDER.NODE_ITEM_LIST).RemoveChild(item);
                            //deleteNode = item;
                            break;
                        }
                    }

                    break;

                }

                this.SaveXML();
                return true;

            }
        }
    }
}
